//
//	HOME AUTOMATION GATEWAY PROJECT
//
//	IOBusApi - Callbacks
//
//  (c) 2008 mocom software GmbH & Co KG
//	for European Microsoft Innovation Center
//
//  $Author: volker $
//  $Date: 2009-09-16 17:39:57 +0200 (Mi, 16. Sep 2009) $
//  $Revision: 496 $
//
//  Microsoft dotNetMF Project
//  Copyright ©2001,2002,2003,2004 Microsoft Corporation
//  One Microsoft Way, Redmond, Washington 98052-6399 U.S.A.
//  All rights reserved.
//  MICROSOFT CONFIDENTIAL
//
//	IOBUS is (c) 2009 mocom software GmbH & Co KG
//
//-----------------------------------------------------------------------------

#include "save/hag.h"
#include "save/hag_mocom_IOBus_IOBusApi.h"
#define DEF_NO_BASIC_TYPES
#include <iobus.h>



extern void CBDataCallbackAck();
extern void CBErrorCallbackAck();


using namespace mocom::IOBus;

INT32 IOBusApi::Enable( UINT32 param0, HRESULT &hr )
{
	INT32 retVal = 0;
	IOBusEnable( param0 );
	return retVal;
}

INT32 IOBusApi::GetMasterEnabledState( HRESULT &hr )
{
    INT32 retVal = IOBusMasterEnabledState(); 	
    return retVal;
}

void IOBusApi::AckDataCallback( HRESULT &hr )
{
	CBDataCallbackAck();
}

void IOBusApi::AckErrorCallback( HRESULT &hr )
{
	CBErrorCallbackAck();
}

INT32 IOBusApi::GetNodeState( INT32 param0, HRESULT &hr )
{
	INT32 retVal = IOBusMasterGetNodeState( param0 );
	return retVal;
}

INT32 IOBusApi::GetNumberOfObjects( INT32 param0, HRESULT &hr )
{
	INT32 retVal = IOBusMasterGetObjects( param0 );
	return retVal;
}

INT32 IOBusApi::GetClass( INT32 param0, INT32 param1, HRESULT &hr )
{
	INT32 retVal = IOBusMasterGetClass( param0, param1 );
	return retVal;
}

INT32 IOBusApi::GetType( INT32 param0, INT32 param1, HRESULT &hr )
{
	INT32 retVal = IOBusMasterGetType( param0, param1 );
	return retVal;
}

INT32 IOBusApi::GetExponent( INT32 param0, INT32 param1, HRESULT &hr )
{
    INT32 retVal = IOBusMasterGetExponent( param0, param1 );
    return retVal;
}

INT32 IOBusApi::GetPhysicalUnitNum( INT32 param0, INT32 param1, HRESULT &hr )
{
	INT32 retVal = IOBusMasterGetPhysicalUnit( param0, param1 );
	return retVal;
}

INT32 IOBusApi::ReadInt( INT32 param0, INT32 param1, HRESULT &hr )
{
	IOBusObjectValue value;

	value.ivalue = 0;

	if ( IOBusMasterGetType( param0, param1 ) == OBJ_IS_ANALOG )
	{
		hr = CLR_E_INVALID_OPERATION;
	}
	else if ( !IOBusMasterGetValue( param0, param1, &value ) )
	{
		hr = CLR_E_INDEX_OUT_OF_RANGE;
	} 
	return value.ivalue;
}

float IOBusApi::ReadFloat( INT32 param0, INT32 param1, HRESULT &hr )
{
	IOBusObjectValue value;

	value.fvalue = 0.0;

	if ( IOBusMasterGetType( param0, param1 ) != OBJ_IS_ANALOG )
	{
		hr = CLR_E_INVALID_OPERATION;
	}
	else if ( !IOBusMasterGetValue( param0, param1, &value ) )
	{
		hr = CLR_E_INDEX_OUT_OF_RANGE;
	}
	return value.fvalue;
}

void IOBusApi::WriteInt( INT32 param0, INT32 param1, INT32 param2, HRESULT &hr )
{
	IOBusObjectValue value;

	if ( IOBusMasterGetType( param0, param1 ) == OBJ_IS_ANALOG )
	{
		hr = CLR_E_INVALID_OPERATION;
	}
	else
	{
		value.ivalue = param2;
		if ( !IOBusMasterSetValue( param0, param1, &value ) )
			hr = CLR_E_INDEX_OUT_OF_RANGE;
	}
}

void IOBusApi::WriteFloat( INT32 param0, INT32 param1, float param2, HRESULT &hr )
{
	IOBusObjectValue value;

	if ( IOBusMasterGetType( param0, param1 ) != OBJ_IS_ANALOG )
	{
		hr = CLR_E_INVALID_OPERATION;
	}
	else
	{
		value.fvalue = param2;
		if ( !IOBusMasterSetValue( param0, param1, &value ) )
			hr = CLR_E_INDEX_OUT_OF_RANGE;
	}
}

double IOBusApi::Log( double param0, HRESULT &hr )
{
    return log(param0);
}
